<extend name="Base/common_nojhd"/>
<block name="TITLE"><title>活动商品</title></block>
<block name="body">
    <form class="am-form" id='cartform' action="{:U('BuyNow/createOrder')}" method="post">
        <div class="am-list-news-bd">
            <ul class="am-list">
                <if condition='$proCount gt 0'>
                    <volist name='cartPros' id='vo'>
                        <!--缩略图在标题左边-->
                        <li class="am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left">
                            <div class="am-u-sm-4 am-list-thumb ">
                                <a href="{:U('Product/ProductDetails?pid='.$vo['pid'])}"><img src="{$vo.pic}?imageView2/1/w/80/h/80/q/80"/></a>
                            </div>
                            <div class=" am-u-sm-8 am-list-main am-padding-left-0">
                                <h3 class="am-list-item-hd am-margin-bottom-xs">
                                    <a href="{:U('Product/ProductDetails?pid='.$vo['pid'])}" style="font-size: 15px;">{$vo.name}</a>
                                    <span class="am-badge am-badge-secondary">{$vo.quantity} &nbsp;<span>单价:￥{$vo.price}</span></span>
                                    <a href="javascript:void(0)" dataid='{$vo.pid}' onclick="delPro(this);" ><i class="am-icon-close"></i></a>
                                </h3>
                                <div class="am-g am-list-item-text" style="min-height: none;">
                                    <div class="am-input-group am-u-sm-7 ">
                                        <input type="tel" dataid="{$vo.pid}" id='pnum' name='pnum' onchange="changenum(this);" style="width: 48px;" value="{$vo.pnum}">
                                        <span class="am-input-group-label am-text-danger" style="font-weight: bold">￥{$vo.price}</span>
                                    </div>
                                    <div class="am-u-sm-4 am-margin-0 am-padding-horizontal-0"  style="font-size: xx-small;left: -10px">
                                        <div style="color: #666;font-weight: bold">{$vo.num0}{$vo.price0}</div>
                                        <div style="color: #666;font-weight: bold">{$vo.num1}{$vo.price1}</div>
                                        <div style="color: #666;font-weight: bold">{$vo.num2}{$vo.price2}</div>
                                    </div>
                                </div>
                            </div>
                        </li>
                    </volist>
                    <else />
                    <div class="am-panel am-panel-default">
                        <div class="am-panel-bd" style=" text-align: center;">你还没有选择中意的商品哦</div>
                    </div>
                </if>
            </ul>
        </div>
        <div data-am-widget="tabs" class="am-tabs am-tabs-default">
            <ul class="am-tabs-nav am-cf">
                <li class="am-active">
                    <a href="[data-tab-panel-0]">收货地址</a>
                </li>
                <li class="">
                    <a href="[data-tab-panel-1]">备注</a>
                </li>

            </ul>
            <div class="am-tabs-bd">
                <div data-tab-panel-0 class="am-tab-panel am-active" id="divaddresslist">
                    <volist name='cartAddresses' id='vo'>
                        <label class="am-radio">
                            <blockquote>
                                <p><input type="radio" name="addressradio" id='addressradio' value="{$vo.id}" class="am-input-lg" data-am-ucheck>
                                    <a data-id="{$vo.id}" href="#">[{$vo.shopname}]{$vo.city}{$vo.street}</a>
                                    <i class="am-icon-close"></i></p>
                                <small>{$vo.name} {$vo.phone}</small>
                            </blockquote>
                        </label>
                    </volist>
                    <button type="button" class="am-btn am-btn-success" id="doc-prompt-toggle">新增地址</button>
                </div>
                <div data-tab-panel-1 class="am-tab-panel ">
                    <div class="am-form-group">
                        <label for="doc-ta-1">输入订单备注</label>
                        <textarea class="" name='remark' rows="5" id="doc-ta-1"></textarea>
                    </div>
                </div>  
            </div>
        </div>

        <!---地址删除模态框--->
        <div class="am-modal am-modal-confirm" tabindex="-1" id="my-confirm">
            <div class="am-modal-dialog">
                <!--<div class="am-modal-hd">Amaze UI</div>-->
                <div class="am-modal-bd">
                    确定要删除这条记录吗？
                </div>
                <div class="am-modal-footer">
                    <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                    <span class="am-modal-btn suredel" data-am-modal-confirm>确定</span>
                </div>
            </div>
        </div>
        <!------>

        <!--地址新增模态框-->
        <div class="am-modal am-modal-prompt" tabindex="-1" id="my-prompt">
            <div class="am-modal-dialog">
                <div class="am-modal-hd">新建收货地址</div>
                <div class="am-modal-bd">
                    <input type="text" class="am-modal-prompt-input" id="shopName" name='shopName' placeholder="公司/店铺名称">
                    <input type="text" class="am-modal-prompt-input" id="realName" name='realName' placeholder="请输入联系人姓名">
                    <input type="text" class="am-modal-prompt-input" id="phone"  name='phone' placeholder="联系人电话">
                    <select id="province"  name='province' class="am-modal-prompt-input">
                        <option value="江苏省">江苏省</option>   
                    </select>
                    <select id="city"  name='city' class="am-modal-prompt-input">
                        <option value="扬州市">扬州市</option>   
                        <option value="泰州市">泰州市</option>  
                        <option value="江都市">江都市</option>
                        <option value="仪征市">仪征市</option>  
                        <option value="高邮市">高邮市</option> 
                        <option value="宝应市">宝应市</option> 
                        <option value="镇江市">镇江市</option> 
                    </select>
                    <textarea class="am-modal-prompt-input" rows="5" id="street" name='street' placeholder="详细地址"></textarea>
                </div>
                <div class="am-modal-footer">
                    <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                    <span class="am-modal-btn" data-am-modal-confirm>提交</span>
                </div>
            </div>
        </div>



        <div data-am-widget="navbar" class="am-navbar am-cf am-navbar-default "
             id="">
            <ul class="am-navbar-nav am-cf am-avg-sm-4">
                <li>
                    <a href="###" class="">
                        <span class="am-text-lg">合计：{$orderPrice}元</span>
                    </a>
                </li>
                <li >
                <if condition='$proCount gt 0'>
                    <a href="###" class="">
                        <button type="button" onclick="createOrder();" class="am-btn am-btn-danger am-btn-xl">确定下单</button>
                    </a>
                    <else/>
                    <button type="button" onclick="createOrder();" class="am-btn am-btn-danger am-btn-xl am-disabled">确定下单</button>
                </if>
                </li>
            </ul>
        </div>
    </form>
</block>


<block name="script">
    <script>
        function createOrder() {
            var url = "{:U('BuyNow/checkStore')}";
            var params = "";
            $.ajax({
                method: 'post',
                url: url,
                params:params,
                success: function (json) {
                    if (json == 1) {
                        $("#cartform").submit();
                    } else {
                        alert("你选择的商品库存不足，请重复此次下单");
                        window.location.reload();
                    }
                }
            })
        }

        $('#cartform').submit(function () {
            var pnums = document.getElementsByName("pnum");
            var addressradios = document.getElementsByName("addressradio");
            var flag = false;
            for (radio in addressradios) {
                if (addressradios[radio].checked) {
                    flag = true;
                    break;
                }
            }
            for ($i = 0; $i < pnums.length; $i++) {
                if (pnums[$i].value == "") {
                    alert("商品数量必须输入");
                    return false;
                }
            }
            if (!flag) {
                alert("请选择一个收货地址");
                return false;
            }
            alert("已下单");
            return true;
        });

        $(function () {
            $('#divaddresslist').find('.am-icon-close').add('#doc-confirm-toggle').
                    on('click', function () {
                        $('#my-confirm').modal({
                            relatedTarget: this,
                            onConfirm: function (options) {
                                var $link = $(this.relatedTarget).prev('a');
                                var addrid = $link.data('id');
                                var url = "{:U('Cart/delAddr')}";
                                var params = 'addrid=' + addrid;
                                $.ajax({
                                    method: 'post',
                                    url: url,
                                    data: params,
                                    success: function (json) {
                                        window.location.reload();
                                    }
                                })
                            },
                            // closeOnConfirm: false,
                            onCancel: function () {
                            }
                        });
                    });
        });
        $(function () {
            $('#doc-prompt-toggle').on('click', function () {
                $('#my-prompt').modal({
                    relatedTarget: this,
                    onConfirm: function (e) {
                        var flag = true;
                        for (var i = 0; i < e.data.length; i++) {
                            if (e.data[i] == "") {
                                alert("请完善地址信息");
                                flag = false
                                break;
                            }
                        }
                        if (flag) {
                            var params = "shopName=" + e.data[0] + "&realName=" + e.data[1] + "&phone=" + e.data[2] + "&province="+e.data[3]+"&city=" + e.data[4] + "&street=" + e.data[5];
                            var url = "{:U('Cart/addAddr')}";
                            $.ajax({
                                method: 'post',
                                url: url,
                                data: params,
                                success: function (json) {
                                    window.location.reload();
                                }
                            })
                        }
                    },
                    onCancel: function (e) {
                    }
                });
            });
        });
        var changenum = function (o) {
            var num = $(o).val();
            var reg = /^\+?[1-9]\d{0,4}$/;
            if (!num.match(reg)) {
                alert("请输入正确的数量,最大购买99999份");
                window.location.reload();
                return false;
            }
            var pid = $(o).data('id');
            var url = "{:U('BuyNow/changeNum')}";
            var params = "num=" + num + "&pid=" + pid;
            $.ajax({
                method: 'post',
                url: url,
                data: params,
                success: function (json) {
                    if (json != -1) {
                        alert("您选择的商品库存不足,仅剩" + json + "份");
                    }
                    window.location.reload();
                }
            })
        }

        var delPro = function (o) {
            var pid = $(o).attr('dataid');
            var url = "{:U('Cart/delPro')}";
            var params = "pid=" + pid;
            $.ajax({
                method: 'post',
                url: url,
                data: params,
                success: function (json) {
                    window.location.reload();
                }
            })
        }
    </script>
</block>